﻿using DeviceCommons.DataHandling;
using DeviceCommons.DeviceMessages.Abstractions;
using DeviceCommons.DeviceMessages.Models.V1;
using System.Text;

namespace DeviceCommons.DeviceMessages.Serialization.V1.Parsers
{
    public class DeviceMessageChildParser : AbstractMessageParser<IDeviceMessageChild>, IDeviceMessageChildParser
    {
        public DeviceMessageChildParser() : base(new DeviceMessageChild())
        {
        }

        public override IDeviceMessageChild Parser(ReadOnlySpan<byte> bytes)
        {
            IDeviceMessageChild model = new DeviceMessageChild();
            byte childCount = bytes[0];
            List<IDeviceMessageInfo> childList = new List<IDeviceMessageInfo>();
            int currentIndex = 1;

            for (int i = 0; i < childCount; i++)
            {
                byte didLength = bytes[currentIndex];
                currentIndex++;

                string deviceId = "";
                if (didLength > 0)
                {
                    deviceId = Encoding.UTF8.GetString(bytes.ToArray(), currentIndex, didLength);
                    currentIndex += didLength;
                }

                byte deviceType = bytes[currentIndex];
                currentIndex++;

                int readingsDataLength = CalculateReadingsDataLength(bytes, currentIndex);

                if (readingsDataLength > 0)
                {
                    var childDevice = new DeviceMessageInfo
                    {
                        DID = deviceId,
                        DeviceType = deviceType,
                        Reading = DeviceMessageSerializerProvider.InfoReadingsV1Par.Parser(bytes.Slice(currentIndex, readingsDataLength))
                    };

                    childList.Add(childDevice);

                    currentIndex += readingsDataLength;
                }
                else
                {
                    childList.Add(new DeviceMessageInfo
                    {
                        DID = deviceId,
                        DeviceType = deviceType,
                        Reading = new DeviceMessageInfoReadings()
                    });
                }
            }

            model.ChildArray = childList.ToArray();
            Model = model;
            return model;
        }

        public static int CalculateReadingsDataLength(ReadOnlySpan<byte> bytes, int index)
        {
            int startIndex = index;
            if (startIndex >= bytes.Length) return 0;

            byte readingCount = bytes[index];
            index++;

            for (int i = 0; i < readingCount; i++)
            {
                index += 2;

                byte stateCount = bytes[index];
                index++;

                for (int j = 0; j < stateCount; j++)
                {
                    _ = bytes[index + 1];
                    index += 2;

                    int valueLength = DeviceMessageUtilities.GetValueLength(bytes, index - 1);
                    index += valueLength;
                }
            }
            return index - startIndex;
        }
    }
}
